// build.rs

fn main() {
  build2();
  build_with_pkg_config();
}

#[warn(dead_code)]
fn build1() {
  /// The gcc command itself is not portable across platforms. For example it’s unlikely that Windows platforms have gcc, and not even all Unix platforms may have gcc. The ar command is also in a similar situation.
  /// These commands do not take cross-compilation into account. If we’re cross compiling for a platform such as Android it’s unlikely that gcc will produce an ARM executable.
  use std::env;
  use std::path::Path;
  use std::process::Command;
  let out_dir = env::var("OUT_DIR").unwrap();

  // Note that there are a number of downsides to this approach, the comments
  // below detail how to improve the portability of these commands.
  Command::new("gcc")
    .args(&["src/hello.c", "-c", "-fPIC", "-o"])
    .arg(&format!("{}/hello.o", out_dir))
    .status()
    .unwrap();
  Command::new("ar")
    .args(&["crus", "libhello.a", "hello.o"])
    .current_dir(&Path::new(&out_dir))
    .status()
    .unwrap();

  println!("cargo:rustc-link-search=native={}", out_dir);
  println!("cargo:rustc-link-lib=static=hello");
  println!("cargo:rerun-if-changed=src/hello.c");
}

/// require build-dependencies cc
fn build2() {
  cc::Build::new().file("src/hello.c").compile("hello");
  println!("cargo:rerun-if-changed=src/hello.c");
}

extern crate pkg_config;
fn build_with_pkg_config() {
  pkg_config::Config::new().probe("zlib").unwrap();
  println!("cargo:rerun-if-changed=zlib.rs");
}
